# include "stack.h"

Stack * newStack () {
	Stack *new = (Stack *) malloc(sizeof(Stack));
	if (!new) logMessage("util.lists","stack","newStack","Memória Insuficiente",CRITICAL);
	new->list = NULL;
	return new;	
}

LinkedList * stackToLinkedList (Stack *s) {
	if (!s) return NULL;
	else return s->list;
}

Stack * linkedLinkedListToStack (LinkedList *lst) {
	Stack *new = newStack();
	new->list = lst;
	return new;
}

void * pop (Stack *stack) {
	void *data;
	LinkedList *aux;
	if (!stack||!stack->list) return NULL;
	else {
		aux = stack->list;
		stack->list = nextElem(stack->list);
		data = getData(aux);
		free(aux);
		return data;
	}
}

void * top (Stack *stack) {
	if (!stack||!stack->list) return NULL;
	else return getData(stack->list);
}

int push (Stack *stack, void *elem) {
	LinkedList *tmp = insertElemHead(stack->list,elem);
	if (!tmp) return 0;
	stack->list = tmp;
	return 1;
}
